package cmich.cps.scheduler.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import cmich.cps.scheduler.SchedulingSystem;
import cmich.cps.scheduler.data.Preference;
import cmich.cps.scheduler.data.Professor;

/**
 * The Class PreferenceController.
 * 
 * @author Jonathan Kissinger <kissi1j@cmich.edu>
 */
public class PreferenceController implements Controller {
	
	/** The table. */
	private final JTable table;
	
	/** The model. */
	private DefaultTableModel model;
	
	/** The system. */
	private SchedulingSystem system = SchedulingSystem.getInstance();
	
	/** The tab name. */
	private String tabName;

	/**
	 * Instantiates a new preference controller.
	 *
	 * @param table the table
	 * @param professor the professor
	 */
	@SuppressWarnings("serial")
	PreferenceController(JTable table, Professor professor) {
		this.table = table;
		String[] headers = { "Item", "Score" };
		this.model = new DefaultTableModel(new Object[][] {}, headers ) {
			@Override  
		      public Class<?> getColumnClass(int col) {  
		        if(col == 0) {
		        	return String.class;  
		        }
		        else {
		        	return Integer.class;  
		        }
			}
		};
		this.table.setModel(this.model);
		this.tabName = professor.getName();
		this.table.setAutoCreateRowSorter(true);
		this.table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		this.table.getTableHeader().setReorderingAllowed(false);
	}

	/* (non-Javadoc)
	 * @see cmich.cps.scheduler.gui.Controller#populate()
	 */
	public void populate() {
		Professor professor = system.getProfessorByName(tabName);
		model.setRowCount(0);
		for(Preference p : professor.getPreferences()) {
			model.addRow(new Object[] { p.getItem(), p.getValue().toString() });
		}
	}

	/* (non-Javadoc)
	 * @see cmich.cps.scheduler.gui.Controller#addFunctionality()
	 */
	public void addFunctionality() {
		model.addTableModelListener(new TableModelListener() {
			@Override
			public void tableChanged(TableModelEvent e) {
				switch (e.getType()) {
				case TableModelEvent.INSERT:
					//Unused
					break;
				case TableModelEvent.UPDATE:
					int row = e.getFirstRow();
					int col = e.getColumn();
					Object object = model.getValueAt(row, col);
					if(!system.modifyPreference(tabName, row, col, object)) {
						populate();
					}
					break;
				case TableModelEvent.DELETE:
					//Unused
					break;
				}
			}
		});
		table.addMouseListener(new TableMenu());
		table.getParent().addMouseListener(new TableMenu());
	}

	/**
	 * @author Jonathan Kissinger <kissi1j@cmich.edu>
	 * 
	 * The Class TableMenu.
	 */
	private class TableMenu extends MouseAdapter {
		
		/* (non-Javadoc)
		 * @see java.awt.event.MouseAdapter#mouseReleased(java.awt.event.MouseEvent)
		 */
		@Override
		public void mouseReleased(MouseEvent e) {
			if (e.isPopupTrigger()) {
				JPopupMenu popup = new JPopupMenu();
				JMenuItem addItem = new JMenuItem("Add new preference");
				addItem.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						if(system.addPreference(tabName)) {
							populate();
						}
					}
				});
				popup.add(addItem);
				int row = table.rowAtPoint(e.getPoint());
				if (row >= 0 && row < table.getRowCount()) {
					final int index = table.convertRowIndexToModel(row);
					Professor professor = system.getProfessorByName(tabName);
					Preference preference = professor.getPreferences().get(index);
					JMenuItem deleteItem = new JMenuItem("Delete " + professor.getName() + "'s preference for " + preference.getItem());
					deleteItem.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent e) {
							system.removePreference(tabName, index);
							model.removeRow(index);
						}
					});
					popup.add(deleteItem);
					table.setRowSelectionInterval(row, row);
				} else {
					table.clearSelection();
				}
				JMenuItem closeItem = new JMenuItem("Close this tab");
				closeItem.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						Professor professor = system.getProfessorByName(tabName);
						Frame.getInstance().closeTab(professor.getName());
					}
				});
				popup.add(closeItem);
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		}
	}
}